package com.ddwl.data.mq.receive;

import com.alibaba.fastjson.JSONObject;
import com.ddwl.common.config.DingTalkNotify;
import com.ddwl.common.constant.RabbitMqConstants;
import com.ddwl.data.model.OperateLog;
import com.ddwl.data.service.OperateLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author hemiaojun
 * @Date 2020/11/10 16:03
 */
@Slf4j
@Component
public class OperateLogReceive {

    @Resource
    private OperateLogService operateLogService;
    @Resource
    private DingTalkNotify dingTalkNotify;

    @RabbitListener(bindings ={@QueueBinding(value = @Queue(value = RabbitMqConstants.OPERATE_LOG_QUEUE,durable = "true"),
            exchange =@Exchange(value = RabbitMqConstants.OPERATE_LOG_EXCHANGE),key = RabbitMqConstants.OPERATE_LOG_QUEUE)})
    public void saveOperateLog(String vo){
        log.info("操作日志 OperateLog: {}", vo);
        try {
            operateLogService.save(JSONObject.parseObject(vo, OperateLog.class));
        } catch (Exception e) {
            log.error("OperateLogReceive.saveOperateLog error:{}",e);
            dingTalkNotify.exceptionNotify(e);
        }
    }

}
